//
//  ViewController.swift
//  SwiftyImageDemo
//
//  Created by 李煜 on 2024/5/14.
//

import SwiftyImage
import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let imageView = UIImageView(frame: .init(x: 20, y: 100, width: 100, height: 100))
        imageView.image = UIImage.size(width: 100, height: 100)
            .color(.white)
            .border(color: .red)
            .border(width: 10)
            .corner(radius: 20)
            .image
        view.addSubview(imageView)

        // 2
        imageView.image = UIImage.size(width: 50, height: 50)
            .color(.yellow)
            .corner(radius: 10)
            .image

        // 3.resize image
        let button = UIButton(frame: CGRect(x: 0, y: 300, width: 100, height: 44))
        button.setBackgroundImage(
            UIImage.resizable()
                .color(.white)
                .border(width: 10)
                .corner(radius: 15)
                .border(color: .orange)
                .image
            , for: .normal)
        view.addSubview(button)

        // 4.
        let background = UIImage.size(width: 120, height: 120)
            .color(.black)
            .corner(radius: 13.5)
            .image
        let circle = UIImage.size(width: 106, height: 106)
            .color(.white)
            .corner(radius: 50)
            .image
        let center = UIImage.size(width: 6, height: 6)
            .color(.black)
            .corner(radius: 3)
            .image
        let clock = background + circle + center

        let finalImage = clock.with { ctx in
            ctx.setLineCap(.round)
            UIColor.black.setStroke()
            ctx.setLineWidth(2)
            
            ctx.move(to: CGPoint(x: clock.size.width / 2, y: clock.size.height / 2))
            ctx.addLine(to: CGPoint(x: clock.size.width / 2 - 5, y: 15))

            ctx.move(to: CGPoint(x: clock.size.width / 2, y: clock.size.height / 2))
            ctx.addLine(to: CGPoint(x: clock.size.width - 25, y: clock.size.height / 2 - 3))

            ctx.strokePath()

            UIColor.red.setStroke()
            ctx.setLineWidth(1)

            ctx.move(to: CGPoint(x: clock.size.width / 2 + 8, y: clock.size.height / 2 - 7))
            ctx.addLine(to: CGPoint(x: 26, y: clock.size.height / 2 + 32))

            ctx.strokePath()

            UIColor.red.setFill()
            let rect = CGRect(x: clock.size.width / 2 - 1, y: clock.size.height - 1, width: 2, height: 2)
            ctx.fillEllipse(in: rect)
            
        }
        
        imageView.image = finalImage
        view.backgroundColor = .systemMint
    }
}

@available(iOS 17.0, *)
#Preview {
    ViewController()
}
